package com.sgb.kjwl.view.ui.activity.enterprise;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPerfessionalTeamBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseModule;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.view.ui.fragment.enterprise.ProfessionalTeamFragment;
import com.sgb.kjwl.viewmodel.enterprise.ProfessionalTeamActivityModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class ProfessionalTeamActivity extends AppActivity {
    @Inject
    ProfessionalTeamActivityModel viewModel;

    private ActivityPerfessionalTeamBinding binding;
    private ArrayList<Fragment> fragmentList;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseComponent.builder().appComponent(appComponent)
                .enterpriseModule(new EnterpriseModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        super.initView();
        binding = DataBindingUtil.setContentView(this, R.layout.activity_perfessional_team);

        viewModel.getTradeCategry();

        fragmentList = new ArrayList<>();
    }

    public void showTabList(List<IpPickSelectEntity<List<IpPickSelectEntity>>> list) {

        String[] tabs = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            tabs[i] = list.get(i).getName();
            this.fragmentList.add(ProfessionalTeamFragment.newInstance(list.get(i).getValue(), list.get(i).getChildren()));
        }
        SlidingFragmentPagerAdapter pagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), fragmentList, tabs);
        binding.vpProfessionalTeam.setCurrentItem(0);
        binding.vpProfessionalTeam.setOffscreenPageLimit(tabs.length);
        binding.vpProfessionalTeam.setAdapter(pagerAdapter);
        binding.tlProfessionalTeam.setupWithViewPager(binding.vpProfessionalTeam);
        binding.tlProfessionalTeam.setTabMode(fragmentList.size() > 3 ? TabLayout.MODE_SCROLLABLE : TabLayout.MODE_FIXED);
        binding.tlProfessionalTeam.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                ((ProfessionalTeamFragment) fragmentList.get(tab.getPosition())).dismissWindow();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ((ProfessionalTeamFragment) fragmentList.get(tab.getPosition())).dismissWindow();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
